<?

function IncludeClass($classname, $dir){
    preg_match("#([\w]+)$#si", $classname, $match);
    $class = $match[1];
    $filename = $dir."/$class.class.php";
    $ok = N;
    if(file_exists($filename)){
        require_once $filename;
        if(class_exists($classname)){
            $ok = Y;
        }
    }
    return $ok;
}

function IncludeLangFile($filename){
    $dirname = dirname($filename);
    $file = str_replace($dirname, "", $filename);
    $dirname = dirname($filename)."/lang/".$GLOBALS['MAIN']->site->language.$file;
    if(file_exists($dirname)){
        include $dirname;
        if(isset($MESS)){
            $GLOBALS["MESS"] = array_merge($GLOBALS["MESS"], $MESS);
        }
    }    
}

function dump() {
    print "<div class='message system'>DUMP:<br />";
    foreach (func_get_args() as $arg) {
        print "<pre style='overflow: auto; z-index: 1001 !important; text-align: left !important; background: #000 !important; color: #0a0 !important; padding: 1ex !important; margin: 1ex !important; border: 1px solid #900 !important'>";
        print_r($arg);
        print "</pre>";
    }
    print "</div>";
}


function d() {
    foreach (func_get_args() as $arg) {
        print "<pre style='overflow: auto; z-index: 1001 !important; text-align: left !important; background: #000 !important; color: #0a0 !important; padding: 1ex !important; margin: 1ex !important; border: 1px solid #900 !important'>";
        print_r($arg);
        print "</pre>";
    }
}


function t($str, $replacers = array()) {
    if (isset($GLOBALS['MESS'][$str]))
        $str = $GLOBALS['MESS'][$str];
    if (!empty($replacers)) {
        foreach ($replacers as $pattern => $replace) {
            $str = str_replace($pattern, t($replace), $str);
        }
    }
    return $str;
}


function getQueryString($params = array()) {
    $get = $_GET;
    $getStr = array();
    $array = array();

    foreach ($params as $key => $param) {
        if (isset($get[$key]))
            unset($get[$key]);
        $array[] = "$key=$param";
    }


    foreach ($get as $key => $val) {
        $getStr[] = "$key=$val";
    }

    $getStr = array_merge($getStr, $array);
    if(preg_match("#\?#",$_SERVER['REQUEST_URI'])){
        return "&".implode("&", $getStr);
    }else{
        return "?".implode("&", $getStr);
    }
}

function pathToUri($path) {
    $path = str_replace("\\", "/", $path);
    return str_replace(ROOT_DIR, "", $path);
}


function check_session() {
    print "<input type=\"hidden\" name=\"session_id\" value=\"" . session_id() . "\">";
}

function set_action($action) {
    print "<input type=\"hidden\" name=\"action\" value=\"$action\">";
}

function check_action($action, $type = false) {
    $ok = false;
    switch ($type) {
        case 'get':
            $ok = (isset($_GET['action']) && $_GET['action'] == $action);
            break;
        case 'post':
            $ok = (isset($_POST['action']) && $_POST['action'] == $action);
            break;
        default : $ok = (isset($_REQUEST['action']) && $_REQUEST['action'] == $action);
    }
    return $ok;
}


function Tree($data, $key_name, $pid_name, $clildren_array_name, $use_empty = false, $pid = 0) {
    
    $result = array();
    if (!empty($data)) {
        foreach ($data as $index => $element) {
            if ($element[$pid_name] == $pid) {
                unset($data[$index]);
                $children = Tree($data, $key_name, $pid_name, $clildren_array_name, $use_empty, $element[$key_name]);
                if (!empty($children)) {
                    $element[$clildren_array_name] = $children;
                }elseif($use_empty == true){
                    $element[$clildren_array_name] = array();
                }
                $result[] = $element;
            }
        }
    }
    return $result;/**/
}


function ThemeList($data, $ul, $li, $elem, $link, $useCount = false){
    //d($data, $ul, $li, $elem, $link);
    $str = "";
    foreach($data as $item){
        $str .= "<li class=\"$li\">";
        if(!empty($link['href'])){
            $field = $link['href'];
            $href = !empty($item['fields'][$field]['value']) ? $item['fields'][$field]['value'] : $item['fields'][$field]['default_value'];
        }else{
            $href = "javascript:;";
        }
        $count = $useCount ? ($item['count'] > 0 ? "<span class=\"count\">".$item['count']."</span>" : false) : false;
        $str .= "<a href=\"$href\" class=\"$elem\">".$item['title'].$count."</a>";
        if(!empty($item['sections'])){
            $str .= ThemeList($item['sections'], $ul, $li, $elem, $link);
        }
        $str .= "</li>";
    }
    
    return "<ul class=\"$ul\">".$str."</ul>";
}